package diploma.kisel.modelas.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Domain class for User (table - app_user).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "app_user")
@NamedQueries({
		@NamedQuery(name = "User.findAll", query = "select u from User u"),
		@NamedQuery(name = "User.findById", query = "select u from User u where u.id = :id") })
public class User {

	private String login;
	private String password;
	private String name;
	private String surname;
	private String group;

	// for many to many mapping
	private Set<Role> roles = new HashSet<Role>();

	// for one to many mappint
	private Set<Project> projects = new HashSet<Project>();

	public User() {

	}

	public User(String login, String password, String name, String surname,
			String group) {
		this.login = login;
		this.password = password;
		this.name = name;
		this.surname = surname;
		this.group = group;
	}

	@Id
	@Column(name = "id_user")
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	@Column(name = "user_password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "user_name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "user_surname")
	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	@Column(name = "user_group")
	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	@ManyToMany
	@JoinTable(name = "app_user_has_role", joinColumns = @JoinColumn(name = "id_user"), inverseJoinColumns = @JoinColumn(name = "id_role"))
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
	public Set<Project> getProjects() {
		return projects;
	}

	public void setProjects(Set<Project> projects) {
		this.projects = projects;
	}

	@Override
	public String toString() {
		return "User [login=" + login + ", password=" + password + ", name="
				+ name + ", surname=" + surname + ", group=" + group + "]";
	}

}
